package com.apes.framework.plugin.esb.processor.jpa;

import com.apes.framework.plugin.esb.api.Request;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.esb.processor.AbstractChainProcessor;
import com.apes.framework.plugin.esb.processor.ChainProcessor;
import com.apes.framework.plugin.esb.processor.Processor;
import com.apes.framework.plugin.esb.processor.router.FilterProcessor;
import com.apes.framework.util.MapUtil;
import com.apes.framework.util.ReflectUtil;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：JPA查询权限组
 *
 * @author xul
 * @create 2022-01-06 14:29
 */
@Data
public class AuthsProcessor<T extends Request, R> extends JpaAuthProcessor<T, R> {
    private String operator = "and";  //"or", "and"
    private List<ChainProcessor> auths = new ArrayList();

    public void addAuth(ChainProcessor auth) {
        auths.add(auth);
    }

    @Override
    protected R doProcess(T source) {
        String postId = ((SimpleRequest) source).getPostId();
        if (StringUtils.isEmpty(postId)) return null;
        Map result = source.getMap();
        if (result.containsKey("condition")) {
            Map group = new HashMap();
            group.put("operator", "and");
            List<Map<String, Object>> filters = (List<Map<String, Object>>) result.get("condition");
            if (!filters.isEmpty()) {
                group.put("filters", filters);
            }
            Map g = createGroup(postId);
            if (g != null) {
                List groups = new ArrayList<>();
                groups.add(g);
                group.put("groups", groups);
            }
            result.remove("condition");
            result.put("group", group);
        } else if (result.containsKey("group")) {
            Map group = (Map) result.get("group");
            Map g = createGroup(postId);
            if (g != null) {
                result.put("group", MapUtil.mapper("groups", new Map[]{group, g}, "operator", "and"));
            }
        }
        return (R) result;
    }

    public Map createGroup(String postId) {
        Map group = null;
        List<Map> filters = null;
        List groups = null;
        for (ChainProcessor auth : auths) {
            if (auth instanceof AuthProcessor) {
                JpaAuthProcessor processor = new JpaAuthProcessor();
                ReflectUtil.copyAll(auth, processor);
                Map filter = processor.getFilter(postId);
                if (filter == null) continue;
                if (filters == null) filters = new ArrayList<>();
                filters.add(filter);
            } else if (auth instanceof AuthsProcessor) {
                Map g = ((AuthsProcessor) auth).createGroup(postId);
                if (groups == null) groups = new ArrayList<>();
                groups.add(g);
            }
        }
        if (filters != null || groups != null) {
            group = new HashMap();
            group.put("operator", operator);
        }
        if (filters != null) {
            group.put("filters", filters);
        }
        if (groups != null) {
            group.put("groups", groups);
        }
        return group;
    }
}
